<?php

class User_model extends S_Model {
	
	public function get_admin_count()
	{
		$result = $this->db->select('count(*) as count')->from($this->table.' as a')->join('role as b', 'a.role_id = b.id')->where('b.parent_id = 1')->get()->row_array();
		return intval($result['count']);
	}
	
	public function get_admin_page($p, $per_page = 10)
	{
		$fields = array_keys($this->fields);
		if ($fields)
		{
			foreach ($fields as &$field)
			{
				$field = 'a.'.$field;
			}
		}
		$fields[] = 'b.name as role_name';
		$fields = implode(',', $fields);
		$query = $this->db->select($fields)->from($this->table.' as a')->join('role as b', 'a.role_id = b.id')->where('b.parent_id = 1')->order_by($this->primary_key.' DESC')->limit($per_page, ($p-1)*$per_page)->get();
		$this->_num_rows = $query->num_rows();
		return $query->result_array();	
	}
	
	public function get_page($p, $per_page = 10)
	{
		$fields = array_keys($this->fields);
		if ($fields)
		{
			foreach ($fields as &$field)
			{
				$field = 'a.'.$field;
			}
		}
		$fields[] = 'b.name as role_name';
		$fields = implode(',', $fields);
		$query = $this->db->select($fields)->from($this->table.' as a')->join('role as b', 'a.role_id = b.id')->order_by($this->primary_key.' DESC')->limit($per_page, ($p-1)*$per_page)->get();
		$this->_num_rows = $query->num_rows();
		return $query->result_array();	
	}

}